using UnityEngine;
using UnityEngine.UI;

public class UIA_PlayerInfo_Script : MonoBehaviour
{
    [Header("最大血量")]
    public float maxHealth = 1000f;
    [Header("血条增减速度")]
    public float speed = 30f;

    private RectTransform healthRT;
    private Text healthText;
    private float hor;
    private float maxHealthWidth;

    private void Awake()
    {
        healthRT = transform.GetChild(0).transform.GetChild(0).gameObject.GetComponent<RectTransform>();
        maxHealthWidth = healthRT.sizeDelta.x;
        healthText = transform.GetChild(0).transform.GetChild(1).gameObject.GetComponent<Text>();
        healthText.text = maxHealth.ToString("0.0");
    }

    void Update()
    {
        hor = Input.GetAxis("Horizontal");
        // 限制血条的范围
        float currentHealth = healthRT.sizeDelta.x + hor * Time.deltaTime * speed;
        currentHealth = Mathf.Clamp(currentHealth, 0, maxHealthWidth);
        // 操控血条
        healthRT.sizeDelta = new Vector2(currentHealth, healthRT.sizeDelta.y);
        // 显示血条文本
        healthText.text = (currentHealth/maxHealthWidth * maxHealth).ToString("0.0");
    }
}